package com.sunday.aomicaichang.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.widgets.taosty.Toasty;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.constValues.ConstValue;
import com.sunday.aomicaichang.model.MenuDetail;
import com.sunday.aomicaichang.ui.MenuDetailActivity;
import com.sunday.member.http.MemberClient;
import com.sunday.member.utils.SignUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/7.
 */

public class MenuDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<MenuDetail.ProductsBean> productsBeen;

    public MenuDetailAdapter(Context mContext) {
        this.mContext = mContext;

    }

    public void setmData(List<MenuDetail.ProductsBean> productsBeen) {
        this.productsBeen = productsBeen;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_menu_detail, parent, false);
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final MenuDetail.ProductsBean products = productsBeen.get(position);
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        viewHolder.name.setText(products.getName());
        viewHolder.num.setText(products.getNum()+products.getStandard());
        viewHolder.car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCart(products);
            }
        });
    }
    private void addCart(MenuDetail.ProductsBean products){
        String nonce= MD5Utils.random();
        String[] str1=new String[]{"productIds","villageId","nums","nonce"};
        int villageId=SharePerferenceUtils.getIns(mContext).getInt(ConstValue.AREA,0);
        String[] str2=new String[]{String.valueOf(products.getId()),String.valueOf(villageId),String.valueOf(products.getNum()),nonce};
        ((MenuDetailActivity)mContext).showLoadingDialog(0);
        Call<ResultDO> call= MemberClient.getApiAdapter().addCart(villageId,String.valueOf(products.getId()),
                String.valueOf(products.getNum()),nonce, SignUtil.getSign(str1,str2));
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                ((MenuDetailActivity)mContext).dissMissDialog();
                if(response.body()==null){return;}
                if (response.body().getCode()==0){
                    Toasty.success(mContext,"成功加入购物车").show();
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                Toasty.error(mContext,"网络连接错误").show();
            }
        });
    }
    @Override
    public int getItemCount() {
        return productsBeen == null ? 0 : productsBeen.size();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.num)
        TextView num;
        @Bind(R.id.car)
        TextView car;

        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
